home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Learn Microsoft Visual Basic 6.0 Now
/
Learn Microsoft Visual Basic 6.0 Now (Microsoft Press)(X03-58607)(1998).ISO
/
media
/
chap09
/
b09a010.cc2
< prev
next >
Wrap
Text File
|
1998-06-07
|
2KB
|
54 lines
0, In a Visual Basic program, you can
2, create local and public variables. A local
6, variable is only available to the
8, procedure it is declared in. It cannot be used
11, by another procedure's program code.
14, However, a pubic variable is available to
17, all the procedures in a program. Any
20, procedure can reference it or assign it a new
22, value. The word programmers use to
26, describe the range or influence of a
28, variable is scope. A variable declared in one
33, event procedure has a scope that is local
35, to that procedure. This isolation
39, protects local variables from being
40, inadvertantly modified by another routine, but it
44, also limits their usefulness. Local
47, variables are good for counters in ForàNext
50, loops and for receiving input. But,
52, they can't be used to store important
54, global values, such as the running total of
56, an invoice. A public variable has a scope
61, that is global throughout the program.
64, Any procedure can reference or modify
66, the contents of a public variable. First,
71, you declare the variable as Public in
72, the declarations section of a standard
74, module. Then, you can initialize the public
77, variable in the Form_Load event
79, procedure or another location, and assign it a
82, value in one or more event procedures.
86, Because the scope of a public variable is
87, global, it maintains its value between
90, calls to an event procedure. This makes
93, public variables useful for recording
95, running totals or tallying the number of
98, wins for a player in a game. In a
102, kitchen, the scope of variables can be
104, illustrated by cookies and a bag of colored
106, chocolate chips. If you'd like to add red
109, chocolate chips to all your cookies, you
111, would add them to the mixing bowl as you
113, prepare the cookie dough. This is the
116, equivalent of using a public variable
119, because it spreads red chocolate throughout
121, the entire batch. If you want to dress
124, up just a few cookies, you can add blue
127, chocolate chips right from the bag before
129, you bake them. This is the equivalent
131, of using a local variable, because the
133, blue chips you use will be limited to just
135, one cookie sheet. As you write larger
138, programs, you'll want to use both local
141, and public variables.
144, END